package com.educal.Controller;

import com.educal.Repository.StuRepository;
import com.educal.Bean.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by miyang on 2017/12/6.
 */
@RestController
public class StuController {

    @Autowired
    private StuRepository stuRepository;
    /*
    * 增加学生*/
   @PostMapping(value = "/students")
    public Student addStu(@RequestParam("stuId") String stuId,
                          @RequestParam("stuName") String stuName,
                          @RequestParam("stuCollege") String stuCollege,
                          @RequestParam("stuClass") String stuClass){
        Student stu = new Student();

        stu.setStuid(stuId);
        stu.setStuname(stuName);
        stu.setStucollege(stuCollege);
        stu.setStuclass(stuClass);
        //System.out.print(stu.getStuid()+ stu.getStuname());

        return stuRepository.save(stu);
    }
    /*@PostMapping(value = "/students")
    public Map<String,Integer> addStu(@RequestParam("stuId") String stuId,
                          @RequestParam("stuName") String stuName,
                          @RequestParam("stuCollege") String stuCollege,
                          @RequestParam("stuClass") String stuClass) {
        Student stu = new Student();
        int flag = 400;

        Map<String, Integer> stum = new HashMap<String, Integer>();
        stu.setStuid(stuId);
        stu.setStuname(stuName);
        stu.setStucollege(stuCollege);
        stu.setStuclass(stuClass);
        stu = stuRepository.save(stu);
        //System.out.print(stu.getStuid()+ stu.getStuname());
        if (stu.hashCode() != 0) {
            stum.put("code", 200);
        }
        return stum;
    }*/

    /*
    * 查询全部学生信息*/
    @GetMapping(value= "/students")
    public List<Student> findAll() {

        return stuRepository.findAll();
    }

    /*
    * 用id查询*/
    @GetMapping(value = "/students/{stuId}")
    public Student findByStuId(@PathVariable("stuId") String stuId) {
        Student stu = new Student();
        List<Student> list = stuRepository.findAll();
        for(int i=0;i<list.size();i++){
            String sId = list.get(i).getStuid();
            if(sId.equals(stuId)){
                stu.setStuid(sId);
                stu.setStuname(list.get(i).getStuname());
                stu.setStucollege(list.get(i).getStucollege());
                stu.setStuclass(list.get(i).getStuclass());
                break;
            }
        }
        return stu;
    }
    /*
    * 用班级查询(报错！！！！！)*/
    @GetMapping(value = "/students/class/{stuClass}")
        public Map<Integer,Student> findByClass(@PathVariable("stuClass") String stuClass) {
        Map<Integer,Student> stuMap = new HashMap<Integer,Student>();
        int k = 0;
        List<Student> list = stuRepository.findAll();
        System.out.print(list);
        for(int i=0;i<list.size();i++){
            String sClass = list.get(i).getStuclass();
            if(sClass.equals(stuClass)){
                /*stuList.get(k).setStuid(list.get(i).getStuid());
                System.out.print(list.get(i).getStuid());
                stuList.get(k).setStuname(sClass);
                stuList.get(k).setStucollege(list.get(i).getStucollege());
                stuList.get(k).setStuclass(list.get(i).getStuclass());
                k++;*/
                stuMap.put(k,list.get(i));
                k++;
            }
        }
        return stuMap;
    }


    /*
    * 修改学生信息*/
    @PutMapping(value = "/students")
    public Student updateStuId(@RequestParam("stuId") String stuId,
                               @RequestParam("stuName") String stuName,
                               @RequestParam("stuCollege") String stuCollege,
                               @RequestParam("stuClass") String stuClass){
        Student stu = new Student();
        stu.setStuid(stuId);
        stu.setStuname(stuName);
        stu.setStucollege(stuCollege);
        stu.setStuclass(stuClass);

        return stuRepository.save(stu);
    }

    /*
    * 用id删除学生*/
    @DeleteMapping(value = "/students/{stuId}")
    public void deleteStu(@PathVariable("stuId") String stuId){
        Student stu = new Student();
        List<Student> list = stuRepository.findAll();
        for(int i=0;i<list.size();i++){
            String sId = list.get(i).getStuid();
            if(sId.equals(stuId)){
                stu.setStuid(sId);
                stu.setStuname(list.get(i).getStuname());
                stu.setStucollege(list.get(i).getStucollege());
                stu.setStuclass(list.get(i).getStuclass());
                stuRepository.delete(stu);
                break;
            }
        }
    }
}
